1
2
3
4
5 package com.jguild.jrpm.test;
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.io.PipedInputStream;
11 import java.io.PipedOutputStream;
12 import com.jguild.jrpm.io.cpio.*;
13
14 import junit.framework.TestCase;
15
16 /***
17 * Tests cpio input/output streams.
18 *
19 * @author kuss
20 */
21 public class CPIOTestCase extends TestCase {
22
23 private static final int MAX_COUNT = 8;
24 private static final String testString = "This is my special test string.";
25 private static final String testDir = "testdirectory/foo/bar";
26 private static final int UID_OFFSET = 10000;
27 private static final int GID_OFFSET = 20000;
28 private static final int TIME_OFFSET = 17000;
29 private OutputStream pOut;
30 private InputStream pIn;
31
32
33
34
35 public void setUp() throws IOException {
36 PipedOutputStream pipeOut = new PipedOutputStream();
37 PipedInputStream pipeIn = new PipedInputStream();
38 pipeIn.connect(pipeOut);
39 pOut = pipeOut;
40 pIn = pipeIn;
41 }
42
43
44
45
46 public void tearDown() {
47 System.gc();
48 Thread.yield();
49 }
50
51 /***
52 * Tests the new cpio format.
53 *
54 * @throws IOException if an I/O error has occurred
55 */
56 public void testNewFormat() throws IOException {
57 cpioTest(CPIOConstants.FORMAT_NEW);
58 }
59
60 /***
61 * Tests the new cpio format including crc check for each file.
62 *
63 * @throws IOException if an I/O error has occurred
64 */
65 public void testNewFormatCrc() throws IOException {
66 cpioTest(CPIOConstants.FORMAT_NEW_CRC);
67 }
68
69 /***
70 * Tests the old binary cpio format.
71 *
72 * @throws IOException if an I/O error has occurred
73 */
74 public void testOldBinaryFormat() throws IOException {
75 cpioTest(CPIOConstants.FORMAT_OLD_BINARY);
76 }
77
78 /***
79 * Tests the old ascii cpio format.
80 *
81 * @throws IOException if an I/O error has occurred
82 */
83 public void testOldAsciiFormat() throws IOException {
84 cpioTest(CPIOConstants.FORMAT_OLD_ASCII);
85 }
86
87 private void cpioTest(final short format) throws IOException {
88 Thread t = new Thread("CPIO Out") {
89 public void run() {
90 try {
91 CPIOOutputStream cpOut = new CPIOOutputStream(pOut, format);
92 String tmpString = testString;
93 for (int count = 0; count < MAX_COUNT; count++) {
94 CPIOEntry cpioEntry = new CPIOEntry();
95 cpioEntry.setMode(CPIOConstants.C_ISREG);
96 cpioEntry.setName(testDir + count);
97 cpioEntry.setUID(UID_OFFSET + count);
98 cpioEntry.setGID(GID_OFFSET + count);
99 cpioEntry.setNumberOfLinks(count);
100 cpioEntry.setTime(TIME_OFFSET + count);
101 tmpString += tmpString + count;
102 cpioEntry.setFileSize(tmpString.length());
103 if (format == CPIOConstants.FORMAT_NEW_CRC) {
104 long crc = 0;
105 byte tmpBuf[] = tmpString.getBytes();
106 for (int pos = 0; pos < tmpBuf.length; pos++) {
107 crc += tmpBuf[pos] & 0xFF;
108 }
109 cpioEntry.setChksum(crc);
110 }
111 cpOut.putNextEntry(cpioEntry);
112 cpOut.flush();
113 Thread.yield();
114 System.out.println("writing "
115 + tmpString.getBytes().length / 1024
116 + " KBytes");
117 cpOut.write(tmpString.getBytes());
118 cpOut.flush();
119 Thread.yield();
120 }
121 cpOut.finish();
122 cpOut.close();
123 } catch (IOException e) {
124 fail(e.toString());
125 }
126 }
127 };
128 t.start();
129 CPIOInputStream cpIn = new CPIOInputStream(pIn);
130 CPIOEntry readEntry;
131 String tmpString = testString;
132 int count = 0;
133 while ((readEntry = cpIn.getNextEntry()) != null) {
134 tmpString += tmpString + count;
135 assertEquals("Format", readEntry.getFormat(), format);
136 assertTrue("Should be a regular file", readEntry.isRegularFile());
137 assertEquals("Name", readEntry.getName(), testDir + count);
138 assertEquals("UID", readEntry.getUID(), UID_OFFSET + count);
139 assertEquals("GID", readEntry.getGID(), GID_OFFSET + count);
140 assertEquals("NumberOfLinks", readEntry.getNumberOfLinks(), count);
141 assertEquals("Time", readEntry.getTime(), TIME_OFFSET + count);
142 assertEquals("FileSize", readEntry.getFileSize(), tmpString
143 .length());
144
145 int tmp;
146 StringBuffer buf = new StringBuffer();
147 while ((tmp = cpIn.read()) != -1) {
148 buf.append((char) tmp);
149 }
150 assertEquals("File", buf.toString(), tmpString);
151 count++;
152 }
153 cpIn.close();
154 }
155 }